package sdk.webview.fmc.com.fmcsdk.util.nfc;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * nfc存储消息实体
 * @author Evan
 *
 */
public class NFCEntity {
	
	public static final String TYPE_ASSET = "ASSET"; //设备类型
	public static final String TYPE_LOCATION = "LOCATION"; //位置类型
	public static final String VERSION = "version";
	public static final String SPLITCHAR = "|"; //数据分隔符
	
	public static final String EVENT_START = "&"; //事件开始标识符
	
	/**
	 * 位置或设备及其它类型
	 * 0表示设备，1表示位置
	 */
	private String type;
	/**
	 * 编号
	 */
	private String number;
	/**版本号*/
	private String version;
	/**
	 * 存储的事件次数,默认3次
	 */
	private int eventCount;
	
	/**
	 * 设备或位置的事件列表
	 */
	private List<Event> eventList;
	
	private SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
	
	public NFCEntity() {
		type = TYPE_ASSET;
		number = "";
		version = "";
		eventCount = 3;
		eventList = new ArrayList<Event>();
	}
	
	public int getEventCount() {
		return eventCount;
	}

	public void setEventCount(int eventCount) {
		this.eventCount = eventCount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getNumber() {
		return number;
	}

	public void setNumber(String number) {
		this.number = number;
	}

	public String getVersion(){ return version;}

	public void setVersion(String version){ this.version = version;}

	public List<Event> getEventList() {
		return eventList;
	}

	public void setEventList(List<Event> eventList) {
		this.eventList = eventList;
	}

	public NFCEntity parse(String payload){
		
		String[] sections = payload.split(EVENT_START);
		String base = sections[0];
		
		//类型
		String type = base.substring(0, 1);
		if(type.equals("0")){
			setType(TYPE_ASSET);
		}else if(type.equals("1")){
			setType(TYPE_LOCATION);
		}
		int index = base.indexOf(SPLITCHAR);
		if (index != -1){
			//编号
			setNumber(base.substring(1,index));
			//版本号
			setVersion(base.substring(index+1));
		}else{
			setNumber(base.substring(1));
		}

		String[] events = new String[]{};
		if(sections.length > 1){
			events = sections[1].split("\\" + SPLITCHAR);
		}
		
		for(int i = 0; i < events.length; i++){
			String event = events[i];
			String eventtype = event.substring(0, 1);
			String time = event.substring(1);
			Event eventEntity = new Event();
			eventEntity.type = eventtype;
			try {
				eventEntity.time = format.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
			eventList.add(eventEntity);
		}
		return this;
	}
	
	@Override
	public String toString() {
		//格式： 类型编号|版本号&事件类型事件时间|事件类型事件时间...
		StringBuffer sb = new StringBuffer();
		if(type.equals(TYPE_ASSET)){
			sb.append("0");
		}else if(type.equals(TYPE_LOCATION)){
			sb.append("1");
		}
		
		sb.append(number);
		if (!TextUtils.isEmpty(version)){
			sb.append(SPLITCHAR).append(version);
		}
		//添加事件信息
		if(eventList.size()>0){
			sb.append(EVENT_START);
			for (Event event : eventList) {
				sb.append(event.type).append(format.format(event.time)).append(SPLITCHAR);
			}
			//去除最后一个SPLITCHAR
			sb.deleteCharAt(sb.length()-1);
		}
		return sb.toString();
	}
	
	/**
	 * 事件实体类
	 * @author Evan
	 *
	 */
	class Event{
		String type;
		Date time;
	}
	
}
